//
//  LCZAVViewController.m
//  PersonalNews
//
//  Created by Golibyo on 15/11/26.
//  Copyright © 2015年 changshuhua. All rights reserved.
//

#import "LCZAVViewController.h"
#import "LCZCollectionViewCell.h"
#import "LCZTableHeadViewController.h"
#import "LCZNetWorkTools.h"
#import "LCZFMDetailViewController.h"
#import "LCZMovieCollectionCell.h"
#import "LCZFullScreenMovieController.h"
@interface LCZAVViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, LCZCollectionViewCellDelegate, LCZMovieCollectionCellDelegate>
@property(nonatomic, strong)LCZBaseCollectionView *collectionView;

@property(nonatomic, strong)NSMutableArray *cListArr;



@property(nonatomic, strong)NSMutableArray *movieTableArr;

@property(nonatomic, strong)NSMutableArray *movieCollectionArr;

@property(nonatomic, assign)CGFloat begainTemp;

@property(nonatomic, assign)CGFloat endTemp;



@end

@implementation LCZAVViewController

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    self.navigationController.navigationBar.hidden = NO;
    if ([LCZSingleton shareSingleton].willFullScreenAppear != YES) {
            [[LCZAVPlayer shareSingleton].player pause];
            [[LCZAVPlayer shareSingleton] removeFromSuperview];
    }

}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
//     NSLog(@"begain%f", self.collectionView.contentOffset.x);
    self.begainTemp = self.collectionView.contentOffset.x;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSLog(@"%f", self.collectionView.contentOffset.x);
    NSLog(@"%f", self.begainTemp);
    if (self.collectionView.contentOffset.x == 0) {
        self.begainTemp = 0;
    }
    if (self.begainTemp != self.collectionView.contentOffset.x) {
        [LCZSingleton shareSingleton].isClick = NO;
        [LCZSingleton shareSingleton].SmallMovieWindow.hidden = YES;
            [[LCZAVPlayer shareSingleton].player pause];
            [[LCZAVPlayer shareSingleton] removeFromSuperview];
    }
//    [[LCZSingleton shareSingleton].AVPlayerView.player pause];
//    [[LCZSingleton shareSingleton].AVPlayerView removeFromSuperview];
    




}



- (void)viewDidLoad {
    [super viewDidLoad];
//    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Left"
//                                                                             style:UIBarButtonItemStylePlain
//                                                                            target:self
//                                                                            action:@selector(presentLeftMenuViewController:)];
    self.view.backgroundColor = [UIColor yellowColor];
    [self createCollectionView];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeSystem];
    leftButton.frame = CGRectMake(10, 30, 50, 20);
    [leftButton setTitle:@"hahah" forState:UIControlStateNormal];
    [self.view addSubview:leftButton];
    [leftButton addTarget:self action:@selector(presentLeftMenuViewController:) forControlEvents:UIControlEventTouchUpInside];
    
    [self createData];
    
    
    //获取通知中心单例对象
   
    //添加当前类对象为一个观察者，name和object设置为nil，表示接收一切通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notice:) name:@"123" object:nil];
    // Do any additional setup after loading the view.
}

- (void)createData {
    [LCZNetWorkTools getdataWithString:@"http://c.m.163.com/nc/topicset/ios/radio/index.html" block:^(id block) {
        NSDictionary *dic = block;
        self.cListArr = [NSMutableArray array];
        self.cListArr = dic[@"cList"];
        [self.collectionView reloadData];
    }];
    
    [LCZNetWorkTools getdataWithString:@"http://c.m.163.com/nc/video/home/0-10.html" block:^(id block) {
        NSDictionary *dic = block;
        self.movieCollectionArr = [NSMutableArray array];
        self.movieTableArr = [NSMutableArray array];
        self.movieCollectionArr = dic[@"videoSidList"];
        self.movieTableArr = dic[@"videoList"];
        [self.collectionView reloadData];
    }];

}

-(void)notice:(NSNotification *)notification{
    LCZFMDetailViewController *fmVC = [[LCZFMDetailViewController alloc] init];
    [self.navigationController pushViewController:fmVC animated:YES];
    fmVC.tid = notification.userInfo[@"1"];
}

- (void)createCollectionView {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.itemSize = CGSizeMake(WIDTH, HEIGHT / 667.0 * 600);
    flowLayout.minimumLineSpacing = 0;
    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.scrollDirection = 1;
    self.collectionView = [[LCZBaseCollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:flowLayout];

    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    //    self.collectionView.bounces = NO;
    [self.collectionView registerClass:[LCZCollectionViewCell class] forCellWithReuseIdentifier:@"reuse"];

    [self.collectionView registerClass:[LCZMovieCollectionCell class] forCellWithReuseIdentifier:@"movieReuse"];
    self.collectionView.backgroundColor = [UIColor colorWithRed:1 green:0.81 blue:0.86 alpha:1];
    self.collectionView.pagingEnabled = YES;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 2;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 0) {
        LCZMovieCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"movieReuse" forIndexPath:indexPath];
        cell.movieTableArr = self.movieTableArr;
        cell.movieCollectionArr = self.movieCollectionArr;
        cell.delegate =self;
        return cell;
    } else {
        LCZCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse" forIndexPath:indexPath];
        cell.cListArr = self.cListArr;
        cell.delegate = self;
        return cell;

    }
    
}

- (void)pushView {
    LCZFullScreenMovieController *fullVC = [[LCZFullScreenMovieController alloc] init];
    [self presentViewController:fullVC animated:YES completion:^{
        
    }];
}

- (void)tableHeadPush:(NSString *)cid {
    
    LCZTableHeadViewController *thVC = [[LCZTableHeadViewController alloc] init];
    thVC.cid = cid;
    [self.navigationController pushViewController:thVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
